package com.zl.tby.service.impl;

import com.zl.tby.core.base.bean.TbyPageResult;
import com.zl.tby.core.base.util.ToolsUtil;
import com.zl.tby.core.common.R;
import com.zl.tby.dto.TbyCameraInfoQuery;
import com.zl.tby.model.TbyCameraInfo;
import com.zl.tby.repository.TbyCameraInfoRepo;
import com.zl.tby.service.ITbyCameraInfoService;
import com.zl.tby.vo.TbyCameraInfoForm;
import org.noear.solon.annotation.Component;
import org.noear.solon.data.annotation.Tran;
import org.noear.wood.IPage;
import org.noear.wood.annotation.Db;
import org.noear.wood.utils.StringUtils;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 相机信息
 * @author changpeng
 * @version 1.0
 * @since 2024/8/6 20:22
 */
@Component
public class TbyCameraInfoServiceImpl implements ITbyCameraInfoService {

    @Db
    private TbyCameraInfoRepo tbyCameraInfoRepo;

    @Override
    @Tran
    public R<TbyCameraInfo> add(TbyCameraInfoForm form) {
        TbyCameraInfo entity = form.createEntity();
        entity.setCreateBy(ToolsUtil.getLoginUserId());
        entity.setCreateTime(LocalDateTime.now());
        long id = tbyCameraInfoRepo.insert(entity, true);
        entity.setId(id);
        return R.ok(entity);
    }

    @Override
    @Tran
    public R<TbyCameraInfo> update(TbyCameraInfoForm form) {
        TbyCameraInfo entity = this.get(form.getId());
        if (entity == null) {
            return R.failed("数据不存在");
        }
        form.updateEntity(entity);
        entity.setUpdateBy(ToolsUtil.getLoginUserId());
        entity.setUpdateTime(LocalDateTime.now());
        tbyCameraInfoRepo.updateById(entity, false);
        return R.ok(entity);
    }

    @Override
    public R<Void> delete(Long id) {
        // TODO 验证是否被引用
        int flag = tbyCameraInfoRepo.deleteById(id);
        return flag > 0 ? R.ok() : R.failed("删除失败");
    }

    @Override
    public TbyCameraInfo get(Long id) {
        return tbyCameraInfoRepo.selectById(id);
    }

    @Override
    public TbyPageResult<TbyCameraInfo> page(TbyCameraInfoQuery query) {
        IPage<TbyCameraInfo> iPage = tbyCameraInfoRepo.selectPage(query.getOffset(), query.getSize(), mp -> {
            mp.whereTrue()
                    .andIf(StringUtils.isNotEmpty(query.getName()), "name like ?", "%" + query.getName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getCode()), "code = ?", query.getCode());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mp.orderBy(query.getOrder());
            }
        });
        return new TbyPageResult(iPage, query);
    }

    @Override
    public List<TbyCameraInfo> list(TbyCameraInfoQuery query) {
        return tbyCameraInfoRepo.selectList(mapperWhereQ -> {
            mapperWhereQ.whereTrue().andIf(StringUtils.isNotEmpty(query.getName()), "name like ?", "%" + query.getName() + "%")
                    .andIf(StringUtils.isNotEmpty(query.getCode()), "code = ?", query.getCode());
            if (StringUtils.isNotEmpty(query.getOrder())) {
                mapperWhereQ.orderBy(query.getOrder());
            }
        });
    }

    @Override
    public R<Boolean> checkCode(Long id, String code) {
        if (StringUtils.isEmpty(code)) {
            return R.ok(false);
        }
        List<TbyCameraInfo> dbList = this.tbyCameraInfoRepo.selectList(mp -> {
            mp.whereEq("code", code);
        });
        for (TbyCameraInfo data : dbList) {
            if (id == null && data.getCode().equals(code)) {
                return R.ok(true);
            } else if (id != null && data.getCode().equals(code) && id.compareTo(data.getId()) != 0) {
                return R.ok(true);
            }
        }
        return R.ok(false);
    }
}
